public class DistributeCookies {

    public static void main(String[] args) {
        DistributeCookies distributeCookies = new DistributeCookies();
        int[] nums = {15,18,19,5,6,13,15,20};
        System.out.println(distributeCookies.distributeCookies(nums, 3));
    }

    int[] kid;
    int ans = Integer.MAX_VALUE;
    public int distributeCookies(int[] cookies, int k) {
        kid = new int[k];
        process(cookies, 0, k);
        return ans;
    }

    public void process(int[] cookies, int i, int k) {
        if (i == cookies.length) {
            int max = Integer.MIN_VALUE;
            for (int j : kid) {
                max = Math.max(max, j);
            }
            ans = Math.min(ans, max);
        }
        for (int j = 0; j < k; j++) {
            int temp = kid[j];
            kid[j] += cookies[i];
            process(cookies, i + 1, k);
            kid[j] = temp;
        }
    }
}
